import { ipcRenderer, contextBridge } from 'electron'

// 定义允许的IPC通道
const ALLOWED_CHANNELS = [
  'get-notes-list',
  'read-note',
  'save-note',
  'create-note',
  'delete-note',
  'update-note-title',
  'update-note-sort',
  'set-note-as-child',
  'promote-note-to-top'
]

// 暴露必要的API给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  getNotesList: () => ipcRenderer.invoke('get-notes-list'),
  readNote: (notePath) => ipcRenderer.invoke('read-note', notePath),
  saveNote: (data) => ipcRenderer.invoke('save-note', data),
  createNote: () => ipcRenderer.invoke('create-note'),
  deleteNote: (notePath) => ipcRenderer.invoke('delete-note', notePath),
  updateNoteTitle: (data) => ipcRenderer.invoke('update-note-title', data),
  updateNoteSort: (noteSorts) => ipcRenderer.invoke('update-note-sort', noteSorts),
  setNoteAsChild: (data) => ipcRenderer.invoke('set-note-as-child', data),
  promoteNoteToTop: (notePath) => ipcRenderer.invoke('promote-note-to-top', notePath),

  on: (channel, listener) => {
    if (ALLOWED_CHANNELS.includes(channel)) {
      ipcRenderer.on(channel, listener)
    }
  },
  off: (channel, listener) => {
    if (ALLOWED_CHANNELS.includes(channel)) {
      ipcRenderer.off(channel, listener)
    }
  }
})